package com.leinao.media.client;

import com.leinao.media.exception.MediaClientException;
import okhttp3.*;

import java.util.concurrent.TimeUnit;

/**
 * 流媒体SDK客户端
 *
 * @author zhangyu
 * @date 2021/12/2 14:00
 */
public class MediaClient {
    private final String appId;
    private final String priKey;
    private final String host;
    private final OkHttpClient okHttpClient;

    public MediaClient(MediaClient.Builder builder) {
        this.appId = builder.appId;
        this.priKey = builder.priKey;
        this.host = builder.host;
        this.okHttpClient = (new OkHttpClient()).newBuilder()
                .readTimeout(builder.options.getReadTimeOut(), TimeUnit.MILLISECONDS)
                .connectTimeout(builder.options.getConnectTimeOut(), TimeUnit.MILLISECONDS)
                .writeTimeout(builder.options.getWriteTimeOut(), TimeUnit.MILLISECONDS)
                .connectionPool(new ConnectionPool(builder.options.getMaxIdleConnections(), builder.options.getKeepAliveDuration(), TimeUnit.MILLISECONDS))
                .build();
    }

    public static MediaClient.Builder newBuilder() {
        return new MediaClient.Builder();
    }

    public String getAppId() {
        return this.appId;
    }

    public String getPriKey() {
        return this.priKey;
    }

    public String getHost() {
        return this.host;
    }

    public OkHttpClient getOkHttpClient() {
        return this.okHttpClient;
    }

    public String post(String uri, String body) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), body);
        Request request = (new okhttp3.Request.Builder()).url(this.getHost() + uri).post(requestBody).build();
        return this.call(request);
    }

    public String call(Request request) {
        String responseBody = null;
        Response response = null;

        try {
            response = this.okHttpClient.newCall(request).execute();
            if (response != null) {
                if (!response.isSuccessful()) {
                    throw new MediaClientException("http failure: " + response.message(), response.code());
                }

                if (null == response.body()) {
                    throw new MediaClientException("the body of response is empty: " + response.message(), response.code());
                }

                responseBody = response.body().string();
            }
        } catch (MediaClientException var9) {
            throw var9;
        } catch (Exception var10) {
            if (response != null) {
                throw new MediaClientException("Unexpected exception: " + response.message(), response.code(), var10);
            }

            throw new MediaClientException("Unexpected exception", -1, var10);
        } finally {
            if (response != null) {
                response.close();
            }

        }
        return responseBody;
    }

    public static final class Builder {
        private String appId;
        private String priKey;
        private String host;
        private ClientOptions options = ClientOptions.newBuilder().build();

        public Builder() {
        }

        public MediaClient.Builder appId(String appId) {
            this.appId = appId;
            return this;
        }

        public MediaClient.Builder priKey(String priKey) {
            this.priKey = priKey;
            return this;
        }

        public MediaClient.Builder host(String host) {
            this.host = host;
            return this;
        }

        public MediaClient.Builder options(ClientOptions options) {
            this.options = options;
            return this;
        }

        public MediaClient build() {
            return new MediaClient(this);
        }
    }
}
